package com.easydict.core;

import com.easydict.config.DicTypeBeanSerializerModifier;
import com.easydict.config.EasyDictProperties;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 模块配置管理器
 * 负责管理Jackson模块的配置和生命周期
 */
public class ModuleConfigurationManager {
    
    private static final String MODULE_NAME_PREFIX = "EasyDict";
    private static final String MODULE_VERSION = "2.0.0-ENTERPRISE";
    
    private final EasyDictProperties properties;
    private final ConcurrentHashMap<String, ModuleRegistration> registeredModules;
    private final AtomicLong moduleCounter;
    private final ConfigurationValidator validator;
    
    public ModuleConfigurationManager(EasyDictProperties properties) {
        this.properties = properties;
        this.registeredModules = new ConcurrentHashMap<>();
        this.moduleCounter = new AtomicLong(0);
        this.validator = new ConfigurationValidator();
    }
    
    public String getModuleName() {
        long moduleId = moduleCounter.incrementAndGet();
        return MODULE_NAME_PREFIX + "-" + moduleId;
    }
    
    public com.fasterxml.jackson.core.Version getModuleVersion() {
        return com.fasterxml.jackson.core.Version.unknownVersion();
    }
    
    public void registerModule(Module module, DicTypeBeanSerializerModifier modifier) {
        String moduleId = generateModuleId(module);
        
        ModuleRegistration registration = new ModuleRegistration(
                module, modifier, System.currentTimeMillis());
        
        registeredModules.put(moduleId, registration);
        
        if (properties.getDebug().isEnabled()) {
            logModuleRegistration(moduleId, registration);
        }
    }
    
    public ModuleRegistration getModuleRegistration(String moduleId) {
        return registeredModules.get(moduleId);
    }
    
    public boolean validateConfiguration() {
        return validator.validate(properties);
    }
    
    public int getRegisteredModuleCount() {
        return registeredModules.size();
    }
    
    private String generateModuleId(Module module) {
        return module.getModuleName() + "@" + System.identityHashCode(module);
    }
    
    private void logModuleRegistration(String moduleId, ModuleRegistration registration) {
        // 这里可以添加日志记录逻辑
        // 为了避免依赖日志框架，暂时留空
    }
    
    public static class ModuleRegistration {
        private final Module module;
        private final DicTypeBeanSerializerModifier modifier;
        private final long registrationTime;
        
        public ModuleRegistration(Module module, 
                                DicTypeBeanSerializerModifier modifier, 
                                long registrationTime) {
            this.module = module;
            this.modifier = modifier;
            this.registrationTime = registrationTime;
        }
        
        public Module getModule() {
            return module;
        }
        
        public DicTypeBeanSerializerModifier getModifier() {
            return modifier;
        }
        
        public long getRegistrationTime() {
            return registrationTime;
        }
        
        public long getUptime() {
            return System.currentTimeMillis() - registrationTime;
        }
    }
    
    private static class ConfigurationValidator {
        
        public boolean validate(EasyDictProperties properties) {
            if (properties == null) {
                return false;
            }
            
            // 验证缓存配置
            if (properties.getCache().getMaxSize() <= 0) {
                return false;
            }
            
            // 验证字典后缀
            if (properties.getDictSuffix() == null || properties.getDictSuffix().trim().isEmpty()) {
                return false;
            }
            
            return true;
        }
    }
}
